//
//  MHLoginViewController.m
//  MHO
//
//  Created by papo on 2/24/14.
//  Copyright (c) 2014 maihoang. All rights reserved.
//

#import "MHLoginViewController.h"
#import "MHRegisterViewController.h"

#import "Lib.h"
#import "Contants.h"
#import "Account.h"
#import "UIImageView+WebCache.h"
#import "MHAppDelegate.h"

#import "MHNavibarView.h"

@interface MHLoginViewController ()
{
    MHNavibarView* navibarView;
}
@property (weak, nonatomic) IBOutlet UITextField *email;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@end

@implementation MHLoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    navibarView = [MHNavibarView createWithTitle:@"Đăng nhập" leftButton:Nil rightButton:Nil searchButton:NO];
    [navibarView setActionSelectorDelegate:self left:@selector(leftButtonPressed:) right:Nil search:Nil];
    [navibarView setRootViewController:self];
    [self.view addSubview:navibarView];
    
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    [self.view addGestureRecognizer:tap];
    
    self.scrollView.contentSize = self.scrollView.frame.size;
}

- (void)tapGesture: (UIGestureRecognizer*)tap {
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITextField Delegate 
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    float offset = 216;
    CGRect frame = self.scrollView.frame;
    if (self.view.frame.size.height - frame.size.height - frame.origin.y < offset) {
        frame.size.height -= offset;
    }
    self.scrollView.frame = frame;
    CGPoint curOffset = self.scrollView.contentOffset;
    CGRect tFrame = textField.frame;
    if (tFrame.origin.y - curOffset.y < 0) {
        [self.scrollView setContentOffset:CGPointMake(0, textField.frame.origin.y) animated:YES];
    }
    else if (tFrame.origin.y + tFrame.size.height - curOffset.y > frame.size.height) {
        [self.scrollView setContentOffset:CGPointMake(0, tFrame.origin.y - frame.size.height + tFrame.size.height) animated:YES];
    }
}


- (void)textFieldDidEndEditing:(UITextField *)textField {
    float offset = 216;
    CGRect frame = self.scrollView.frame;
    if (self.view.frame.size.height - frame.size.height - frame.origin.y >= offset) {
        frame.size.height += offset;
    }
    self.scrollView.frame = frame;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
#pragma mark - Button Action
- (IBAction)signInPressed:(id)sender {
    NSString* email = _email.text;
    NSString* password = strCheckNull(_password.text);
    
    if (strCheckNull(email).length == 0) {
        [Lib showMessage:@"Bạn chưa nhập email!" title:@""];
        return;
    }
    
    NSDictionary* dict = @{kAccountEmail:email, kAccountPassword: password };
    [Lib showLoadingView];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        ServiceResult* result = [Account loginWithInfo:dict];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [Lib removeLoadingView];
            [[MHAppDelegate sharedApp].viewController showHomeVc];
            [[MHAppDelegate sharedApp].viewController updateUserInfo];
            if (result.success) {
                [Lib showMessage:@"Đăng nhập thành công!" title:@""];
                [Lib setString:password forKey:@"paccount"];
            }
            else {
                [Lib showMessage:result.message title:@""];
            }
        });
    });
}
- (IBAction)signUpPressed:(id)sender {
//    [self.view endEditing:YES];
    [[MHAppDelegate sharedApp].viewController showRegisVc];
}

- (void)leftButtonPressed: (id)sender {
    [self.view endEditing:YES];
    [[MHAppDelegate sharedApp].viewController showLeftPanelAnimated:YES];
}

@end
